package com.lej.core.bus

import androidx.lifecycle.LifecycleOwner
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.jeremyliao.liveeventbus.LiveEventBus
import com.jeremyliao.liveeventbus.core.Observable
import com.lej.core.utils.extention.fromJson
import com.lej.core.utils.extention.toJson


enum class Events {
    SHARE_TO_SMALL_APP,//分享到小程序
    WEB_UPDATE_TOKEN,//webView刷新token
    ;

    val event: Observable<String> = LiveEventBus.get(name, String::class.java)

    fun postT(any: Any?) {
        event.post(any?.toJson())
    }

    inline fun <reified T> observeT(owner: LifecycleOwner, crossinline block: (T?) -> Unit) {
        event.observe(owner) { block(it.fromJson<T>()) }
    }

    fun post(value: String? = null) {
        event.post(value ?: "")
    }

    fun observe(owner: LifecycleOwner, block: (String) -> Unit) {
        event.observe(owner) { block(it) }
    }
}

